<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
      label-width="80px">
      <el-row>
        <el-form-item label="属性名称" prop="specsName">
          <el-input v-model="queryParams.specsName" placeholder="请输入商品属性名称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>


        <el-form-item label="状态" prop="dataStatus">
          <el-select v-model="queryParams.dataStatus" placeholder="请选择状态" clearable size="small">
            <el-option v-for="dict in dataStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item style="margin-left: 22px;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row class="row-mbinfo">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['product:property:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['product:property:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['product:property:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="divTable">
      <el-table border v-loading="loading" :data="specsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="商品属性名称" align="center" prop="specsName" :show-overflow-tooltip="true" />
        <el-table-column label="状态" align="center" prop="dataStatus" :formatter="dataStatusFormat">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.dataStatus" active-value="1" inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="dataPriority" :show-overflow-tooltip="true" sortable />
        <el-table-column label="修改时间" align="center" prop="updateTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['product:property:edit']">修改</el-button>
            <el-button v-if="scope.row.dataStatus == '0' " size="mini" type="text" icon="el-icon-delete"
              @click="handleDelete(scope.row)" v-hasPermi="['product:property:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改商品属性/属性对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="属性名称" prop="specsName">
          <el-input v-model="form.specsName" placeholder="请输入商品属性名称" class="select-normal" maxlength="10"
            show-word-limit />
        </el-form-item>
        <el-form-item label="排序" prop="dataPriority">
          <el-input-number v-model="form.dataPriority" placeholder="请输入排序" controls-position="right" :min="0"
            class="select-normal" :max="999999" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import {
    listProperty,
    getProperty,
    delProperty,
    addProperty,
    updateProperty,
    updatePropertyStatus
  } from "@/api/product/property";

  export default {
    name: "Property",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        //选中数组的名字
        names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 商品属性/属性表格数据
        specsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态字典
        dataStatusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          platformUid: null,
          specsName: null,
          specsType: null,
          dataStatus: null,
          dataPriority: null,
          updateUid: null,
          updateTime: null
        },
        columns: [{
            label: '商品属性ID',
            dataIndex: 'specsUid',
          },
          {
            label: '平台ID',
            dataIndex: 'platformUid',
          },
          {
            label: '商品属性名称',
            dataIndex: 'specsName',
          },
          {
            label: '类型(1属性/0属性)',
            dataIndex: 'specsType',
          },
          {
            label: '状态（1）有效',
            dataIndex: 'dataStatus',
          },
          {
            label: '排序',
            dataIndex: 'dataPriority',
          },
          {
            label: '修改人ID',
            dataIndex: 'updateUid',
          },
          {
            label: '修改时间',
            dataIndex: 'updateTime',
          },
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          specsName: [{
            required: true,
            message: "属性名称不能为空",
            trigger: "blur"
          }],
          dataPriority: [{
            required: true,
            message: "排序不能为空",
            trigger: "blur"
          }]
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_data_status").then(response => {
        this.dataStatusOptions = response.data;
      });
    },
    methods: {
      /** 查询商品属性/属性列表 */
      getList() {
        this.loading = true;
        listProperty(this.queryParams).then(response => {
          this.specsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态字典翻译
      dataStatusFormat(row, column) {
        return this.selectDictLabel(this.dataStatusOptions, row.dataStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          specsUid: null,
          platformUid: null,
          specsName: null,
          specsType: null,
          dataStatus: null,
          dataPriority: 10,
          updateUid: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.specsUid)
        this.names = selection.map(item => item.specsName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        //用于禁用删除
        if (this.multiple == false) {
          for (var i = 0; i < selection.length; i++) {
            var element = selection[i];
            if ("1" === element.dataStatus) {
              this.multiple = true;
              break;
            }
          }
        }
      },
      // 状态修改
      handleStatusChange(row) {
        let text = row.dataStatus === "0" ? "无效" : "有效";
        this.$confirm('确定要把属性名称为"' + row.specsName + '"设为' + text + '吗？', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          console.log(row.dataStatus);
          return updatePropertyStatus(row.specsUid, row.dataStatus);
        }).then(() => {
          this.msgSuccess("操作成功");
          this.getList();
        }).catch(function() {
          row.dataStatus = row.dataStatus === "0" ? "1" : "0";
        });
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加商品属性";
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const specsUid = row.specsUid || this.ids
        getProperty(specsUid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改商品属性";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.specsUid != null) {
              updateProperty(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addProperty(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const specsUids = row.specsUid || this.ids;
        const names = row.specsName || this.names;
        this.$confirm('是否确认删除商品属性名称为"' + names + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProperty(specsUids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('product/specs/export', {
          ...this.queryParams
        }, `product_specs.xlsx`)
      }
    }
  };
</script>
